package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.CheckUploadMapper;
import org.dey.pojo.*;
import org.dey.service.CheckUploadService;
import org.dey.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author deyran
 * @classname
 * @see ServiceImpl
 */
@Service
@Transactional
public class CheckUploadServiceImpl extends ServiceImpl<CheckUploadMapper,CheckUpload> implements CheckUploadService {

    @Autowired
    CheckUploadMapper checkuploadMapper;
    @Autowired(required = false)
    MemberService memberService;


    @Override
    public PageVo<CheckUpload> querycheckupload(CheckUpload checkupload, int pageNum, int pagesize) {

        //组装条件
        QueryWrapper<CheckUpload> queryWrapper = new QueryWrapper<CheckUpload>();

/*        //查出体检套餐信息
        QueryWrapper<CheckGroup> checkqueryWrapper = new QueryWrapper<CheckGroup>();
        checkqueryWrapper.eq("id",checkupload.getCid());
        CheckGroup checkrone = CheckGroupService.getOne(checkqueryWrapper);
        checkupload.setCname(checkrone.getName());
*/

        //根据档案号模糊查询
        if(StringUtils.isNotBlank(checkupload.getFileNumber())) {
            queryWrapper.or().like("FILE_NUMBER", checkupload.getFileNumber());
        }
        if(StringUtils.isNotBlank(checkupload.getName())) {
            //根据手机号模糊查询
            queryWrapper.or().like("PHONE_NUMBER", checkupload.getPhoneNumber());
        }
        //根据名字模糊查询
        if(StringUtils.isNotBlank(checkupload.getName())) {
            queryWrapper.or().like("NAME", checkupload.getName());
        }

        PageHelper.startPage(pageNum, pagesize);
        List<CheckUpload> checkUploadList = this.list(queryWrapper);

        for (CheckUpload checkload : checkUploadList) {
            Integer cid = checkload.getCid();
            CheckGroup check = checkuploadMapper.getcheck(cid);
            checkload.setCname(check.getName());
        }

        PageInfo<CheckUpload> pageInfo = new PageInfo<CheckUpload>(checkUploadList);
        PageVo<CheckUpload> pageVo =new PageVo<CheckUpload>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());
        return pageVo;
    }


    @Override
    public Result insertcheckupload(CheckUpload checkupload) {
        Result result =null;
        Member member = memberService.getById(checkupload.getMid());
        checkupload.setName(member.getName());
        checkupload.setFileNumber(member.getFileNumber());
        checkupload.setPhoneNumber(member.getPhoneNumber());

        Integer cid = checkupload.getCid();
        CheckGroup check = checkuploadMapper.getcheck(cid);
        checkupload.setCname(check.getName());

        boolean one = this.save(checkupload);

        if(one){
            result = new Result(true, "新增报告成功");
        }else {
            result = new Result(true, "新增报告失败");
        }
        return result;
    }

    @Override
    public Result delcheckupload(int id) {
        Result result =null;
        boolean b = this.removeById(id);
        if(b){
            result = new Result(true, "删除报告成功");
        }else {
            result = new Result(true, "删除报告失败");
        }
        return result;
    }

    @Override
    public CheckUpload querycheckuploadbyid(int id) {
        return this.getById(id);
    }

    @Override
    public Result updatecheckuploadbyid(CheckUpload checkupload) {
        Result result =null;
        boolean b = this.updateById(checkupload);
        if(b){
            result = new Result(true, "");
        }else {
            result = new Result(true, "");
        }
        return result;
    }

    @Override
    public Result submitcheckupload(Integer id) {
        Result result =null;
        UpdateWrapper<CheckUpload> uploadUpdateWrapper = new  UpdateWrapper<CheckUpload>();
        uploadUpdateWrapper.set("upstate",1).eq("id",id);
        boolean update = this.update(uploadUpdateWrapper);
        if (update){
            result = new Result(true, MessageConstant.UPLOAD_SUCCESS);
        }else {
            result = new Result(true, "上传失败");
        }
        return result;
    }

    @Override
    public List<Order> queryorderName(){
        List<Order> orders = checkuploadMapper.getordername();
        return null;
    }

    @Override
    public List<CheckGroup> queryallcheck(){
        List<CheckGroup> checks = checkuploadMapper.queryallcheck();
        return checks;
    }


}
